﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Tools.Publishing.Handlers.ExceptionHandler;
using $safeprojectname$.Infrastructure.Factories;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace $safeprojectname$.Publishing.Handlers
{
    [TestClass]
    public class ExceptionConfigReaderTest : TestBase
    {
        [TestMethod]
        public void NullElement_Failed()
        {
            Exception exception = null;
            try
            {
                XElement el = null;
                var config = new ConfigurationReader().Create(el);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Message.Contains("ExceptionConfiguration: The parameter 'configElement' cannot be null"), exception.Message);
        }


        [TestMethod]
        public void IncorrectElementName_Failed()
        {
            Exception exception = null;
            try
            {
                var config = ExceptionConfigurationReader.ForIncorrectElement();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            Assert.IsTrue(exception.Message.Contains("ExceptionConfiguration: The parameter 'configElement' cannot be null"), exception.Message);
        }

        [TestMethod]
        public void BrokenEventId_Failed()
        {
            Exception exception = null;
            try
            {
                var config = ExceptionConfigurationReader.ForBrokenEventId();
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Message.Contains("ExceptionConfiguration: exception element has incorrect attribute id:"), exception.Message);
        }
        [TestMethod]
        public void MissingExceptionType_Failed()
        {
            Exception exception = null;
            try
            {
                var config = ExceptionConfigurationReader.ForMissingType();
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Message.Contains("ExceptionConfiguration: exception element has missing attribute 'type'"), exception.Message);
        }

        [TestMethod]
        public void Default_Passed()
        {
            var config = ExceptionConfigurationReader.ForDefault();
            Assert.IsTrue(config.Exceptions.IsNotEmpty());
            Assert.IsFalse(config.DoNotPublish);
        }

        [TestMethod]
        public void NoExceptionConfigured_Passed()
        {
            var config = ExceptionConfigurationReader.ForExceptionMissing();
            Assert.IsTrue(config.Exceptions.IsEmpty());
            Assert.IsFalse(config.DoNotPublish);
        }

        #region Init

        // You can use the following additional attributes as you write your tests:

        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }

        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }

        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion Init
    }
}
